﻿require "rexml/document"
include REXML

# PDE Headless build工具核心类
# 定义了输入文件的模型，以及一些工具方法
# @author 许祯
module PDEHeadlessUtil

	public
	# 插件工程信息类
	# 一般从feature.xml文件中获取，包含plugin id以及build version两个属性
	# 要求plugin id必须与插件工程目录名一致
	# plugin id用于export代码，以及生成产品集成表
	# build version只用于生成产品集成表
	class PluginInfo
		attr_accessor :m_plugin_id, :m_build_version
		# 
		# @param [String] id , plugin id
		# @param [String] version, plugin build version
		def initialize( id, version )
			@m_plugin_id = id
			@m_build_version = version
		end

		def to_s
			"#{m_plugin_id}.#{m_build_version}"
		end
	end

	##
	# 解析并保存pde headless build的输入信息
	# 输入信息主要包括两个部分:
	# svn location
	#  包括svn地址，需要编译的feature id，该location下的plugins以及features目录,别名，编译类型，svn版本信息
	# 全局设置
	# 全局设置包括时间戳以及指定feature分类定义
	class PDEHeadlessBuildInfo 

		attr_accessor :m_svn_infoes, :m_global_setting
		
		def initialize( svn_locations, global_setting )
			@m_svn_infoes = svn_locations
			@m_global_setting = global_setting
		end
		
		@@PATH_svn_info= "pde_headless_build/svn_infoes/svn_info"
		@@PATH_global_setting = "pde_headless_build/global_setting"
		
		# 
		# 解析输入文件，将输入文件转化为PDEHeadlessBuildInfo对象
		# @param [String] input_file_path, 输入文件的路径
		# @return [PDEHeadlessBuildInfo] 
		def self.parse_input( input_file_path )
			file_input = File.open(input_file_path,"r")
			rexml_doc = Document.new(file_input)

			svn_locations = parse_svn_infoes(rexml_doc) 
			global_setting = parse_global_setting( rexml_doc ) 

			result = PDEHeadlessBuildInfo.new svn_locations, global_setting
			file_input.close
			return result
		end

		private
		# 解析输入文件的全局设置
		# @param [REXML::Document] rexml_doc
		# @return [GlobalSetting] result 
		def self.parse_global_setting( rexml_doc )
			result = GlobalSetting.new
			begin
				if (elems = rexml_doc.get_elements(@@PATH_global_setting) ) != nil
					elem_global_setting = elems[0]
					if elem_global_setting != nil
						if (elems = elem_global_setting.get_elements("build_timestamp") )!= nil
							result.m_build_timestamp = elems[0].get_text.to_s if elems[0] != nil
							if result.m_build_timestamp == "default" or result.m_build_timestamp == "now"
								result.m_build_timestamp = PDEHeadlessUtil.get_now
							end
						end

						if (elems = elem_global_setting.get_elements("username") ) != nil
							result.m_username = elems[0].get_text.to_s if elems[0] != nil
						end

						if (elems = elem_global_setting.get_elements("password") ) != nil
							result.m_password= elems[0].get_text.to_s if elems[0] != nil
						end

					end
				end
			end
			return result
		end
		
		# 解析SVN location部分
		# @param [REXML::Document] rexml_doc
		# @return [Array] svn_infoes链表
		def self.parse_svn_infoes( rexml_doc )
			svn_infoes = []
			rexml_doc.elements.each(@@PATH_svn_info) do |element|
				begin
					url = element.get_elements("url")[0].get_text.to_s
					url_alias = element.get_elements("alias")[0].get_text.to_s
					build_type = element.get_elements("build_type")[0].get_text.to_s
					
					svn_info = SVNInfo.new(url, url_alias, build_type)
					
					elem_revision = element.get_elements("revision")[0]
					svn_info.m_revision = elem_revision.get_text.to_s if elem_revision.get_text != nil

					element.get_elements("build_features/feature_id").each do |feature_id| 
						svn_info.m_feature_IDs << feature_id.get_text.to_s
					end
					
					elem_build_features = element.get_elements("build_features")[0]
					dir_features = elem_build_features.attributes["dir"]
					svn_info.m_dir_features = dir_features if dir_features != nil

					elem_build_plugins = element.get_elements("build_plugins")[0]
					if elem_build_plugins != nil
						dir_plugins = elem_build_plugins.attributes["dir"]
						svn_info.m_dir_plugins = dir_plugins if dir_plugins != nil
					end

					svn_infoes << svn_info 
				end
			end
			return svn_infoes
		end
		
	end
	##
	# 保存相关的SVN信息
	##
	class SVNInfo
		attr_accessor :m_url,:m_url_alias,:m_build_type,:m_revision,:m_dir_features,:m_dir_plugins,:m_feature_IDs
	
		#@param [String] url : svn地址
		#@param [String] url_alias : svn地址别名
		#@param [String] build_type : 编译类型
		#@param [Array]  feature_IDs : 该SVN地址下需要编译的feature id列表
		#@param [String] revision : svn版本, default is HEAD
		#@param [String] dir_features : features所在目录, default is features
		#@param [String] dir_plugins :  plugins所在目录,default is plugins
		def initialize( url, url_alias, build_type, 
					   feature_IDs=[],
					   revision="HEAD",dir_features="features",
					   dir_plugins="plugins" )
			@m_url = url
			@m_url_alias = url_alias
			@m_build_type = build_type
			@m_dir_features = dir_features
			@m_dir_plugins = dir_plugins
			@m_feature_IDs = feature_IDs
		end
	end
		
		# 编译的全局设置
		class GlobalSetting
			attr_accessor :m_build_timestamp, :m_username, :m_password

			# @param [String] build_timestamp, 编译的时间戳
			# @param [String] username, SVN用户名 
			# @param [String] password, SVN密码
			def initialize( build_timestamp=PDEHeadlessUtil.get_now,
							username="default",
							password="default"
						  )
				@m_build_timestamp = build_timestamp
				@m_username = username
				@m_password = password
			end
		end	
	
	module_function
	# 获取当前时间
	# @return [String] 当前时间戳, 格式为: 201201300101
	def get_now
		return Time.now.strftime("%Y%m%d%H%M").to_s
	end

	# 处理路径
	def correct_path( path )
		return path + "/" if path[path.length-1] != "/"
	end

	# 执行命令
	# @param [Array] commands 命令列表
	def execute_commands( commands )
		commands.each do |command|	
			self.execute_command( command )	
		end
	end

	# 执行命令
	# @param [Array] commands 命令列表
	# @raise 执行命令出错将raise
	def execute_command( command )
		## 过滤用户名密码
		puts command[0...command.index("--username")]
		raise if false == system( command )
	end


end

